<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">

      <sticky :z-index="10" :class-name="'sub-navbar '+postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          发布
        </el-button>
        <el-button v-loading="loading" type="warning" @click="draftForm">
          草稿
        </el-button>
      </sticky>

      <div class="createPost-main-container">
        <el-row>
          <el-col :span="24">

            <div class="postInfo-container">
              <el-row>
                <!-- 礼品名称 -->
                <el-col :span="12">
                  <el-form-item label-width="90px" label="礼品名称:" class="postInfo-container-item">
                    <el-input v-model="postForm.name" placeholder="输入名称" style="width: 205px;"></el-input>
                  </el-form-item>
                </el-col>

                <!-- 礼品数量 -->
                <el-col :span="12">
                  <el-form-item label-width="120px" label="礼品数量:" class="postInfo-container-item">
                    <el-input v-model="postForm.number" placeholder="输入数量"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <!-- 发布人 -->
                <el-col :span="12">
                  <el-form-item label-width="90px" label="发布人:" class="postInfo-container-item">
                    <el-select v-model="postForm.user" :remote-method="getRemoteUserList" filterable default-first-option remote placeholder="Search user">
                      <el-option v-for="(item,index) in userListOptions" :key="item+index" :label="item" :value="item" />
                    </el-select>
                  </el-form-item>
                </el-col>

                <!-- 发布时间 -->
                <el-col :span="12">
                  <el-form-item label-width="120px" label="发布时间:" class="postInfo-container-item">
                    <el-date-picker v-model="publishTime" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="发布日期时间" />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <!-- 礼品类型 -->
                <el-col :span="12">
                  <el-form-item label-width="90px" label="礼品类型:" class="postInfo-container-item">
                    <el-select v-model="postForm.attribute" placeholder="选择类型" clearable >
                      <el-option v-for="(item,index) in attributeListOptions" :key="item+index" :label="item" :value="item" />
                    </el-select>
                  </el-form-item>
                </el-col>
                
                <!-- 所需积分 -->
                <el-col :span="12">
                  <el-form-item label-width="120px" label="所需积分:" class="postInfo-container-item">
                    <el-input v-model="postForm.point" placeholder="输入积分"></el-input>
                  </el-form-item>
                </el-col>
                <!-- 发放形式：直接发放至账户/线下领取 -->
                <!-- <el-col :span="12" >
                  <el-form-item label-width="120px" label="发放形式:" class="postInfo-container-item">
                    <el-radio-group v-model="postForm.getway">
                      <el-radio :label='"1"'>账户发放</el-radio>&nbsp;&nbsp;
                      <el-radio :label='"2"'>线下发放</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col> -->
              </el-row>
            </div>
          </el-col>
        </el-row>
        <!-- 上传图片 -->
        <el-form-item label-width="90px" label="礼品图片" prop="image" style="margin-bottom: 30px;">
          <Upload v-model="postForm.image" />
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import Upload from '@/components/Upload/SingleImage'
import Sticky from '@/components/Sticky'
import { validURL } from '@/utils/validate'
import { fetchGiftById } from '@/api/gift'
import { searchUser } from '@/api/remote-search'

const defaultForm = {
  status: 'preparing',
  name: '',
  number:'',
  attribute:'',
  // getway: '',
  image: '', // 图片
  publishTime: undefined, // 展示时间
  id: undefined,
}

export default {
  name: 'GiftDetail',
  components: { Upload, Sticky },

  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
        callback(new Error(rule.field + '为必传项'))
      } else {
        callback()
      }
    }
    return {
      postForm: Object.assign({}, defaultForm),
      loading: false,
      userListOptions: [],
      attributeListOptions: ['礼品券','实物','平台物品'],
      rules: {
        image: [{ validator: validateRequire }],
        name: [{ validator: validateRequire }],
      },
      tempRoute: {}
    }
  },
  computed: {
    publishTime: {
      get() {
        return (+new Date(this.postForm.publishTime))
      },
      set(val) {
        this.postForm.publishTime = new Date(val)
      }
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchGiftData(id)
    }
    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    fetchGiftData(id) {
      fetchGiftById(id).then(response => {
        this.postForm = response.data

      }).catch(err => {
        console.log(err)
      })
    },
   
    submitForm() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$notify({
            title: '成功',
            message: '发布礼品成功',
            type: 'success',
            duration: 2000
          })
          this.postForm.status = 'published'
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    draftForm() {
      if (this.postForm.name.length === 0 ) {
        this.$message({
          message: '请填写必要的礼品名称',
          type: 'warning'
        })
        return
      }
      this.$message({
        message: '保存成功',
        type: 'success',
        showClose: true,
        duration: 1000
      })
      this.postForm.status = 'draft'
    },
    getRemoteUserList(query) {
      searchUser(query).then(response => {
        if (!response.data.items) return
        this.userListOptions = response.data.items.map(v => v.name)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/mixin";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.task-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>